# -*- coding: utf-8 -*-
import os
import commands
import time

class MISERVICECMD:
   did = 0
   cmd_id = 0
   ask_cmd = "小爱同学"
   answer_cmd = "暂无"

   def __init__(self, ask, answer):
      self.message_id = time.time()
      self.ask_cmd = ask
      self.answer_cmd = answer

   def set_cmd_data(self, cmd, is_tts_open):
      cmd_str = "micli.py 5-4 " + cmd
      if TRUE == is_tts_open: 
          cmd_str = cmd_str + " $1"
      else: 
          cmd_str = cmd_str + " $0"
      os.system(cmd_str)
      print('cmd excute success')

   def get_cmd_data(self, cmd):  #查找命令
      print('get cmd data success')

class MISERVICECMDMANAGER:
    data_dict = {}

    def __init__(self, ask, id):
        test_cmd = MISERVICECMD("今天杭州天气怎么样")
        test_cmd.set_cmd_data(test_cmd.ask_cmd, False)

    def find(self, ask):
        for key in sorted(data_dict.keys()):            # yes
            if str(ask) == key:
                print "该存在! " ,key, data_dict[key]
                return key
            else:                                       # no
                print "抱歉，该值不存在！"
        return -1

    def add(self, ask):
        new_cmd = MISERVICECMD(ask)
        data_dict[test_cmd.cmd_id] = new_cmd

    def modify(self, cmd_id, ask_cmd, answer_cmd):
        data_dict[cmd_id].cmd_id = cmd_id
        data_dict[cmd_id].ask_cmd = ask_cmd
        data_dict[cmd_id].answer_cmd = answer_cmd

    def modify_answer(self, ask, answer_cmd):
        cmd_id = self.find(ask)
        if cmd_id != -1:
            data_dict[cmd_id].answer_cmd = answer_cmd

    def remove(self, ask):
        if len(data_dict) > 0:
            cmd_id = self.find(ask)
            if cmd_id != -1:
                del data_dict[cmd_id]
            else:
                 print "抱歉，该值不存在！"
        else:
            data_dict.clear()      # 清空字典所有条目

import os, time
import multiprocessing
import multiprocessing.connection



class MyChildProcess(multiprocessing.Process):
    def __init__(self, conn):
        multiprocessing.Process.__init__(self)
        self.conn = conn

    def run(self):
        recv(self.conn)


if __name__ == '__main__':
    conn1,conn2 = multiprocessing.Pipe()
    p1 = MyChildProcess(conn1)
    p1.start()
    for data in ['hello', 'statelessness', 'retain', 'birth']:
        conn2.send(data)
        rcvdata = conn2.recv()
        print(f'主进程 recv():{rcvdata}')
    time.sleep(4)
    conn2.close()
    p1.join()

‘’'
子进程80808, __name__='__mp_main__' recv: data='hello'
主进程 recv():HELLO
子进程80808, __name__='__mp_main__' recv: data='statelessness'
主进程 recv():STATELESSNESS
子进程80808, __name__='__mp_main__' recv: data='retain'
主进程 recv():RETAIN
子进程80808, __name__='__mp_main__' recv: data='birth'
主进程 recv():BIRTH
子进程长时间未收到数据.
‘''